// src/router/RequireAdmin.tsx

import { useEffect, useState } from 'react';
import { Navigate } from 'react-router-dom';
import type { FC, ReactElement } from 'react';
import { useUserStore } from '@/store/userStore';

interface RequireAdminProps {
  children: ReactElement;
}

const RequireAdmin: FC<RequireAdminProps> = ({ children }) => {
  const { user } = useUserStore();
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    if (user.role !== undefined) {
      setIsLoading(false);
    }
  }, [user.role]);

  if (isLoading) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
      </div>
    );
  }

  return user.role === 'Admin' ? children : <Navigate to="/login" replace />;
};

export default RequireAdmin; 